import re
import pandas as pd
file = r'C:\Users\Desktop\xx.txt'

class_map = re.compile(r'^class-map (.*)')
class_map_vid = re.compile(r"^match svlan-id (.*)")
pw = re.compile(r'^pw (.*)')
peer = re.compile(r"^peer (.*?) vcid (.*)")
vfi = re.compile(r'^vfi (.*)')
mpls = re.compile(r"^mpls xconnect pw (.*)")
des = re.compile(r'^description (.*)')
cip = re.compile(r'^cip (.*)')
service = re.compile(r'^service-type ethernet (.*?) (class_map_vlan.*)')
des1 = re.compile(r'^description (.*)')
xconnect = re.compile(r"^xconnect (.*)")

vid_dic = {}
vid_lst = []
pw_dic = {}
peer_lst = []
vcid_lst = []
vfi_dic = {}
mpls_lst = []
des_lst = []
cip_dic = {}
port_lst = []
class_map_lst = []
des1_lst = []
xconnect_lst = []

i = 0
with open(file) as line:
    for f in line:
        if class_map.search(f):
            m = class_map.search(f)
            i += 1

        if class_map_vid.search(f) and i != 0:
            n = class_map_vid.search(f)
            vid_lst.append(n.group(1))

        if "!" in f and i != 0:
            i = 0
            vid_dic.setdefault(m.group(1), vid_lst)
            vid_lst = []

frame1 = pd.DataFrame.from_dict(vid_dic, orient="index").reset_index().rename(
    columns={'index': "class_map", 0: "class_map_vid"})

i = 0
with open(file) as line:
    for f in line:
        if pw.search(f):
            m = pw.search(f)
            i += 1

        if peer.search(f) and i != 0:
            n = peer.search(f)
            peer_lst.append(n.group(1))
            vcid_lst.append(n.group(2))

        if "!" in f and i != 0:
            i = 0
            pw_dic.setdefault(m.group(1), [peer_lst, vcid_lst])
            peer_lst = []
            vcid_lst = []

frame2 = pd.DataFrame(pw_dic).T.reset_index().rename(columns={'index': "pw", 0: "peer", 1: "vcid"})

i = 0
with open(file) as line:
    for f in line:
        if vfi.search(f):
            m = vfi.search(f)
            i += 1

        if mpls.search(f) and i != 0:
            n = mpls.search(f)
            mpls_lst.append(n.group(1))

        if des.search(f) and i != 0:
            n = des.search(f)
            des_lst.append(n.group(1))

        if "!" in f and i != 0:
            i = 0
            vfi_dic.setdefault(m.group(1), [mpls_lst, des_lst])
            mpls_lst = []
            des_lst = []

frame3 = pd.DataFrame(vfi_dic).T.reset_index().rename(columns={'index': "vfi", 0: "mpls_pw", 1: "des"})

i = 0

with open(file) as line:
    for f in line:

        if cip.search(f):
            m = cip.search(f)
            i += 1

        if service.search(f) and i != 0:
            n = service.search(f)
            port_lst.append(n.group(1))
            class_map_lst.append(n.group(2))

        if des1.search(f) and i != 0:
            n = des1.search(f)
            des1_lst.append(n.group(1))

        if xconnect.search(f) and i != 0:
            n = xconnect.search(f)
            xconnect_lst.append(n.group(1))

        if "!" in f and i != 0:
            i = 0
            cip_dic.setdefault(m.group(1), [port_lst, class_map_lst, des1_lst, xconnect_lst])
            port_lst = []
            class_map_lst = []
            xconnect_lst = []
            des1_lst = []

frame4 = pd.DataFrame(cip_dic).T.reset_index().rename(
    columns={'index': "cip", 0: "port", 1: "class_map", 2: 'description', 3: 'xconnect'})

with pd.ExcelWriter(r'C:\Users\Desktop\xx提取_0713.xlsx') as writer:
    frame1.to_excel(writer, sheet_name='class-map', index=False)
    frame2.to_excel(writer, sheet_name='pw', index=False)
    frame3.to_excel(writer, sheet_name='vfi', index=False)
    frame4.to_excel(writer, sheet_name='cip', index=False)

print("Done")